package com.xia.manager.error;

import org.springframework.boot.autoconfigure.web.BasicErrorController;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ErrorViewResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MyErrorController
 * @Deserintion 自定义错误处理
 * @Author ciyuan
 * @Date 2018/11/25/025 16:07
 **/
public class MyErrorController extends BasicErrorController {

    public MyErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers) {
        super(errorAttributes, errorProperties, errorViewResolvers);
    }

    @Override
    protected Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
        Map<String,Object> map = super.getErrorAttributes(request,includeStackTrace);
        map.remove("timestamp");
        map.remove("status");
        map.remove("error");
        map.remove("exception");
        map.remove("path");
        String errorCode = (String) map.get("message");
        ErrorEnum errorEnum = ErrorEnum.getByCode(errorCode);
        map.put("message",errorEnum.getMessage());
        map.put("code", errorEnum.getCode());
        map.put("canRetry", errorEnum.isCanRetry());
        return map;
    }
}
